/**
 * iVprog, Alice:
 * @author LOB, RR; Ben Buchwald, Dennis Cosgrove
 */

package edu.cmu.cs.stage3.alice.core.question.ask;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import edu.cmu.cs.stage3.alice.core.property.StringProperty;
import edu.cmu.cs.stage3.swing.DialogManager;

// NumberQuestion extends Question extends Expression extends Element
public class AskUserForNumber extends edu.cmu.cs.stage3.alice.core.question.NumberQuestion {

  private static final String str_title = I18n.getString("question"), str_question = I18n.getString("enterNumber");

  // Properties are used in: src/edu/cmu/cs/stage3/alice/authoringtool/util/ElementPrototype.java
  // - 'for (String desiredPropertie:desiredProperties) edu.cmu.cs.stage3.alice.core.Property property=testElement.getPropertyNamed(desiredPropertie)'
  public final StringProperty
     title = new StringProperty( this, "title", str_title), // "Question"
     question = new StringProperty( this, "question", str_question); 

  private edu.cmu.cs.stage3.alice.core.Clock m_clock;

  //Rem @Override
  // Called by: 'edu.cmu.cs.stage3.alice.core.Property.evaluateIfNecessary(...)'
  public Object getValue () {
    // System.out.println("AskUserForNumber.java: aqui 1");
    // String str="";try{ System.out.println("AskUserForNumber.java: aqui 2"+str.charAt(3)); } catch (Exception e) { e.printStackTrace(); }

    if (m_clock != null) {
      m_clock.pause();
      }
    try {
      // String str="";
      try { // String str="";try{ System.out.println("AskUserForNumber.java: aqui 2"+str.charAt(3)); } catch (Exception e) { e.printStackTrace(); }

	// get the personalized message
        String string = DialogManager.showNumericDialog(this, question.getStringValue(), title.getStringValue(), javax.swing.JOptionPane.QUESTION_MESSAGE );

        if (string==null || string.length()==0) return new Double(0);
        return new Double(string);
      } catch (Exception e) {
         System.err.println("Error: AskUserForNumber.java: "+e.toString()); e.printStackTrace();
         return new Double("0");
         }
    } finally {
      if (m_clock != null) {
        m_clock.resume();
        }
      }
    }

  //Rem @Override
  // Finally implemented in: 'edu.cmu.cs.stage3.alice.core.Element'
  protected void started (edu.cmu.cs.stage3.alice.core.World world, double time) {
    super.started( world, time );
    if (world != null) {
      m_clock = world.getClock();
      } 
    }

  //Rem @Override
  // Finally implemented in: 'edu.cmu.cs.stage3.alice.core.Element'
  protected void stopped (edu.cmu.cs.stage3.alice.core.World world, double time) {
    m_clock = null;
    super.stopped( world, time );
    }

  }
